package com.contactbook.web.controller.group;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.contactbook.web.service.GroupClient;
import com.contactbook.web.util.GroupMessage;

import contactbook.model.Account;
import contactbook.model.Group;

public class UpdateGroupController extends HttpServlet implements Servlet {

	private static final long serialVersionUID = 1L;
	
	private static final String CONFIRM_UPDATE = "OK";
	private static final String CANCEL_UPDATE = "Annuler";

	private GroupClient groupClient;
	
	public UpdateGroupController() {
		super();
    	groupClient = new GroupClient();
	}
	
	public void init() throws ServletException {
    	super.init();
    }
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		RequestDispatcher dispatcher = request.getRequestDispatcher("/Index");
		dispatcher.forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession(true);
		Account account = (Account)session.getAttribute("account");
		String submit = request.getParameter("submit");
		RequestDispatcher dispatcher = request.getRequestDispatcher("/Index");
	
		if (account != null) {	
			groupClient.login(account.getLogin(), account.getPassword());
			
			int groupId = Integer.valueOf(request.getParameter("groupId"));
			Group group = groupClient.getGroupById(groupId);
			
			if (submit == null) {
				request.setAttribute("group", group);
				dispatcher = request.getRequestDispatcher("/updateGroup.jsp");
			}
			else if (submit.equals(CONFIRM_UPDATE)) {
				GroupMessage message = updateGroup(request);
				group = groupClient.getGroupById(groupId);
				request.setAttribute("message", message);
				request.setAttribute("group", group);
				dispatcher = request.getRequestDispatcher("updateGroup.jsp");
			}
			else if (submit.equals(CANCEL_UPDATE)) {
				dispatcher = request.getRequestDispatcher("/Group");
			}
			
			groupClient.logout();
		}
		
		dispatcher.forward(request, response);
	}
	
	private GroupMessage updateGroup(HttpServletRequest request) {
		int groupId = Integer.valueOf(request.getParameter("groupId"));
		Group group = groupClient.getGroupById(groupId);
		
		if (group != null) {
			String name = request.getParameter("name");
			String description = request.getParameter("description");
			if (!name.equals("")) {
				group.setName(name);
				group.setDescription(description);
				groupClient.update(group);
				return GroupMessage.GROUP_UPDATED_OK;
			}
			else
				return GroupMessage.GROUP_UPDATED_NAME_FAIL;
		}
		return GroupMessage.GROUP_UPDATED_FAIL;
	}
	
	public void destroy() {
		super.destroy();
	}

}
